﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using pTools.cMethods;

namespace pTools.TuQiangPrint
{
    public partial class NewSelectPurchase : DevExpress.XtraEditors.XtraForm
    {
        U8DbHelper u8Db = new U8DbHelper();
        int rowsPerPage = 20;
        public NewSelectPurchase()
        {
            InitializeComponent();
        }

        private void NewSelectPurchase_Load(object sender, EventArgs e)
        {
            // 加载采购订单列表
            QueryPurchase();
        }

        int totalNum = 0;
        int totalPage = 0;
        int currentPage = 0;
        void QueryPurchase()
        {
            currentPage = int.Parse(tb_pageNumber.Text);

            // cfree1 颜色 cfree3 商标
            var sql_template = @"select 
main.cPOID,main.dPODate,main.cMaker, detail.cInvCode, inv.cInvName,
detail.iQuantity,detail.ivouchrowno,detail.cfree1,detail.cfree3 
from PO_Pomain main left join PO_Podetails detail on main.POID = detail.ID 
left join inventory inv on detail.cInvCode = inv.cInvCode {0}
order by detail.ID desc,detail.ivouchrowno OFFSET {1} ROWS FETCH NEXT {2} ROWS ONLY";

            var cPOID = tb_cPOID.Text;
            var proCode = tb_proCode.Text;
            var proName = tb_proName.Text;
            var dateBegin = tb_date_begin.Text;
            var dateEnd = tb_date_end.Text;
            var color = tb_color.Text;
            var trademark = tb_trademark.Text;

            var whereStr = " where ";

            if (!string.IsNullOrEmpty(cPOID))
            {
                if (whereStr != " where ")
                {
                    whereStr += " and ";
                }
                whereStr += " main.cPOID like '%" + cPOID + "%' ";
            }
            if (!string.IsNullOrEmpty(proCode))
            {
                if (whereStr != " where ")
                {
                    whereStr += " and ";
                }
                whereStr += " detail.cInvCode like '%" + proCode + "%' ";
            }
            if (!string.IsNullOrEmpty(proName))
            {
                if (whereStr != " where ")
                {
                    whereStr += " and ";
                }
                whereStr += " detail.cInvName like '%" + proName + "%' ";
            }

            if (!string.IsNullOrEmpty(dateBegin))
            {
                if (whereStr != " where ")
                {
                    whereStr += " and ";
                }
                whereStr += " main.dPODate >= '" + dateBegin + "' ";
            }

            if (!string.IsNullOrEmpty(dateEnd))
            {
                if (whereStr != " where ")
                {
                    whereStr += " and ";
                }
                whereStr += " main.dPODate <= '" + dateEnd + "' ";
            }

            if (!string.IsNullOrEmpty(color))
            {
                if (whereStr != " where ")
                {
                    whereStr += " and ";
                }
                whereStr += " detail.cfree1 like '%" + color + "%' ";
            }

            if (!string.IsNullOrEmpty(trademark))
            {
                if (whereStr != " where ")
                {
                    whereStr += " and ";
                }
                whereStr += " detail.cfree3 like '%" + trademark + "%' ";
            }

            if (whereStr == " where ")
            {
                whereStr = "";
            }

            var sql = string.Format(sql_template, whereStr, rowsPerPage * (currentPage - 1), rowsPerPage);
            var dt = u8Db.getData(sql);

            // 确定条目总数
            var sql_totalNum_template = @"select count(*) totalNum 
from PO_Pomain main left join PO_Podetails detail on main.POID = detail.ID 
left join inventory inv on detail.cInvCode = inv.cInvCode {0} ";

            var sql_totalNum = string.Format(sql_totalNum_template, whereStr);
            var totalNumStr = u8Db.getData(sql_totalNum).Rows[0]["totalNum"].ToString();

            lb_totalCount.Text = "总数：" + totalNumStr + "条";
            totalNum = int.Parse(totalNumStr);

            // 确定总页数

            if (totalNum % rowsPerPage == 0)
            {
                totalPage = totalNum / rowsPerPage;
            }
            else
            {
                totalPage = totalNum / rowsPerPage + 1;
            }

            lb_totalPage.Text = "/" + totalPage + "页";


            var displayTable = new DataTable();

            displayTable.Columns.Add("cPOID", typeof(string));
            displayTable.Columns.Add("dPODate", typeof(string));
            displayTable.Columns.Add("cmaker", typeof(string));
            displayTable.Columns.Add("cInvCode", typeof(string));
            displayTable.Columns.Add("cInvName", typeof(string));
            displayTable.Columns.Add("iQuantity", typeof(string));
            displayTable.Columns.Add("ivouchrowno", typeof(string));
            displayTable.Columns.Add("cfree1", typeof(string));
            displayTable.Columns.Add("cfree3", typeof(string));

            foreach (DataRow row in dt.Rows)
            {
                var dateStr = "";
                if (!string.IsNullOrEmpty(row["dPODate"].ToString()))
                {
                    dateStr = DateTime.Parse(row["dPODate"].ToString()).ToString("yyyy-MM-dd");
                }

                //var a = row["cPOID"].ToString();
                //a = row["cmaker"].ToString();
                //a = row["cInvCode"].ToString();
                //a = row["cInvName"].ToString();
                //a = decimal.Parse(row["iQuantity"].ToString()).ToString("0.00");
                //a = row["ivouchrowno"].ToString();
                //a = row["cfree1"].ToString();
                //a = row["cfree3"].ToString();

                displayTable.Rows.Add(new object[] { 
                    row["cPOID"].ToString(),
                    dateStr,
                    row["cmaker"].ToString(),
                    row["cInvCode"].ToString(),
                    row["cInvName"].ToString(),
                    row["iQuantity"].ToString(),
                    row["ivouchrowno"].ToString(),
                    row["cfree1"].ToString(),
                    row["cfree3"].ToString()
                });

                displayTable.Columns[0].Caption = "采购订单号";
                displayTable.Columns[1].Caption = "单据日期";
                displayTable.Columns[2].Caption = "制单人";
                displayTable.Columns[3].Caption = "存货编码";
                displayTable.Columns[4].Caption = "存货名称";
                displayTable.Columns[5].Caption = "数量";
                displayTable.Columns[6].Caption = "行号";
                displayTable.Columns[7].Caption = "颜色";
                displayTable.Columns[8].Caption = "商标";

                gridControl1.DataSource = displayTable;
                gridView1.PopulateColumns();
            }
        }

        private void btn_search_Click(object sender, EventArgs e)
        {
            Reset_PageNum_To_1();
            QueryPurchase();
        }

        private void btn_jumpToPage_Click(object sender, EventArgs e)
        {
            QueryPurchase();
        }

        void Reset_PageNum_To_1()
        {
            tb_pageNumber.Text = "1";
            currentPage = 1;
        }

        private void btn_prevPage_Click(object sender, EventArgs e)
        {
            if (currentPage > 1)
            {
                tb_pageNumber.Text = (int.Parse(tb_pageNumber.Text) - 1).ToString();
                QueryPurchase();
            }
        }

        private void btn_nextPage_Click(object sender, EventArgs e)
        {
            if (currentPage < totalPage)
            {
                tb_pageNumber.Text = (int.Parse(tb_pageNumber.Text) + 1).ToString();
                QueryPurchase();
            }
        }

        public List<DataRow> selectedRows = new List<DataRow>();
        private void btn_confirm_Click(object sender, EventArgs e)
        {
            foreach (var index in gridView1.GetSelectedRows())
            {
                selectedRows.Add(gridView1.GetDataRow(index));
            }
            
            Close();
        }
    }
}